/*****
author : searchNode
date: 2016/12/10
******/
import dataC from './data'
import cyMrg from './cyMrg'
import Config from './config'
export default {

    set(lines, flag) {
        for (var i = 0; i < lines.length; i++) {
            if (flag) {
                lines[i].addClass("selected")
            } else {
                lines[i].removeClass("selected")
            }
        }
    },

    find(cd, flag) {
        // newNodeA.connectedEdges()
        let node = cyMrg.findNode(cd.id)
        let allLine = node.connectedEdges()
        for (var i = 0; i < allLine.length; i++) {
            let line = allLine[i].data()
            let source = allLine[i].source()
            let target = allLine[i].target()
            try {
                if (flag) {
                    source.addClass('selected')
                    target.addClass('selected')
                        //cyMrg.dhEdge(line.id, 'visible')
                } else {
                    source.removeClass('selected')
                    target.removeClass('selected')
                        //cyMrg.dhEdge(line.id, 'hidden')
                }
            } catch (e) {}

        }

    },

    find_(data, flag) {
        const id = data.id
        const that = this
        this.setEl('', true)
        if (dataC.edges) {
            for (var i = 0; i < dataC.edges.length; i++) {
                dataC.edges[i]['group'] = 'edges'
                if (dataC.edges[i]['data']['source'].toString() === id.toString() || dataC.edges[i]['data']['target'].toString() === id.toString()) {
                    if (dataC.edges[i]['data']['source'].toString() === id.toString()) {
                        this.setEl(dataC.edges[i]['data']['target'], flag)
                    } else {
                        this.setEl(dataC.edges[i]['data']['source'], flag)
                    }
                    try {
                        if (flag) {
                            dataC.cy.add(dataC.edges[i])
                        } else {
                            that.delEdge(dataC.edges[i].data.id)
                        }
                    } catch (e) {}
                }
            }
        }
    },

    delEdge(id) {
        dataC.cy.edges(function(i, edge) {
            var data = edge.data()
            if (data.id === id) {
                dataC.cy.remove(edge)
            }
        })
    },

    doubleClick(data) {
        //alert(dataC.master)
        Config.topVue.$message(dataC.master)
    },

    setEl(id, flag) {
        dataC.cy.nodes(function(i, node) {
            var data = node.data()

            if (flag) {
                if (!id) {
                    // node.removeClass('eater')
                } else {
                    if (id.toString() === data['id'].toString()) {
                        node.addClass('eater')
                    }
                }
            } else {
                if (!id) {
                    // node.removeClass('eater')
                } else {
                    if (id.toString() === data['id'].toString()) {
                        node.removeClass('eater')
                        console.log('kkkkkkkkkkklllllllllll:', id)
                    }
                }
            }
        })
    }
}